package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.EnterpriseProduct;
import com.fsnbigdata.domain.Enterprise;
import com.fsnbigdata.domain.QEnterpriseProduct;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = EnterpriseProductRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = EnterpriseProductRepositoryCustom.class)
@Transactional(readOnly = true)
public class EnterpriseProductRepositoryImpl extends QueryDslRepositorySupportExt<EnterpriseProduct> implements EnterpriseProductRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    EnterpriseProductRepositoryImpl() {
        super(EnterpriseProduct.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ENTERPRISE = "enterprise";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DESCRIPTION = "description";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY = "category";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String BARCODE = "barcode";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PRODUCT_ID = "productId";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * TODO Auto-generated method documentation
     *
     * @param enterprise
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<EnterpriseProduct> findByEnterprise(Enterprise enterprise, GlobalSearch globalSearch, Pageable pageable) {
        QEnterpriseProduct enterpriseProduct = QEnterpriseProduct.enterpriseProduct;
        JPQLQuery<EnterpriseProduct> query = from(enterpriseProduct);
        Assert.notNull(enterprise, "enterprise is required");
        query.where(enterpriseProduct.enterprise.eq(enterprise));
        Path<?>[] paths = new Path<?>[] { enterpriseProduct.enterprise, enterpriseProduct.productId, enterpriseProduct.name, enterpriseProduct.barcode, enterpriseProduct.category, enterpriseProduct.description };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(ENTERPRISE, enterpriseProduct.enterprise).map(PRODUCT_ID, enterpriseProduct.productId).map(NAME, enterpriseProduct.name).map(BARCODE, enterpriseProduct.barcode).map(CATEGORY, enterpriseProduct.category).map(DESCRIPTION, enterpriseProduct.description);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, enterpriseProduct);
    }
}
